#include "bsp_epit.h"
/*
frac 分频值
value 装载值
*/
void epit1_init(unsigned int frac,unsigned int value)
{
    if(frac > 0XFFF) frac = 0XFFF;
    EPIT1->CR = 0; /* 先清零 CR 寄存器 */   
    EPIT1->CR |= (1<<24);//选择Peripheral clock作为时钟源
    EPIT1->CR |= frac<<4;//设置分频系数
    EPIT1->CR |= (1<<3);//设置LDR位工作在set-and-forget模式
    EPIT1->CR |= (1<<2);//使能EPIT1中断
    EPIT1->CR |= (1<<1);//从LR加载到计数器
    EPIT1->LR = value;//计数值
    EPIT1->CMPR = 0; /* 比较寄存器值*/
    GIC_EnableIRQ(EPIT1_IRQn);//使能EPIT中断
    system_register_irqhandler(EPIT1_IRQn,(system_irq_handler_t)epit1_irqhandler,NULL);//注册中断函数
    EPIT1->CR |= 1<<0; /* 使能 EPIT1 */
}
void epit1_irqhandler(void)
{
    static unsigned char beep_state = 0,led_state=1;
    if(EPIT1->SR & (1<<0)) /* 判断比较事件发生 */
    { 
        led_state = gpio_read(GPIO1,3);
        led_state = !led_state;
		gpio_write(GPIO1,3,led_state);
    }
    EPIT1->SR |= 1<<0; /* 清除中断标志位 */
}
